var currentIndex;
$(document).ready(function(){
	selectCard();
	getTheTag();
	findHotTag();
});
//获取标签信息
function getTheTag(){
	id = window.location.href.split("?")[1];
	var url = "/HyperIT/tag/findTheTag";
	var parm = "id="+id;
	$.get(url,parm,function(data){
		if (data.serviceResult) {
			$(".tag-banner>h1").text(data.resultParm.tag.name);
			$("#createtime").text("诞生时间："+data.resultParm.tag.createtime.substr(0,16));
			$("#articleNum em").eq(1).text(data.resultParm.tag.article);
			$("#questionNum em").eq(1).text(data.resultParm.tag.question);
			initTagArticle();
		} else{
			messageTip(data.resultInfo);
		}
	});
}
//切换选择卡
function selectCard(){
	$("#selectCard li").click(function(){
		$(this).addClass("active").siblings().removeClass("active");
		initCard($(this).index());
	});
}
//选择卡触发不同事件
function initCard(index){
	switch (index){
		case 0:
			initTagArticle();
			break;
		case 1:
			initTagQuestion();
			break;
		default:
			break;
	}
}
function getTagArticle(index){
	var url = "/HyperIT/tag/getArticle";
	var parm = "size=10&indexPageNum="+index+"&tag_id="+id;
	$.get(url,parm,function(data){
		if (data.serviceResult) {
			var articlelist = data.resultParm.user_article;
			showArticle(articlelist);
			currentIndex++;
		} else{
			messageTip(data.resultInfo);
		}
	});
}
function getTagQuestion(index){
	var url = "/HyperIT/tag/getQuestion";
	var parm = "size=10&indexPageNum="+index+"&tag_id="+id;
	$.get(url,parm,function(data){
		if (data.serviceResult) {
			var questionlist = data.resultParm.user_question;
			showQuestion(questionlist);
			currentIndex++;
		} else{
			messageTip(data.resultInfo);
		}
	});
}
//绘制article
function showArticle(list){
	for(var i = 0; i<list.length; i++){
		var tags = "";
		for(var j = 0; j<list[i].tag.length; j++){
			tags = tags + '<a href="'+getRealPath()+'/views/tag/thetag.html?'+list[i].tag[j].id+'"><span>'+list[i].tag[j].name+'</span></a>';
		}
		$(".list .btn-box").before('<div class="article-item item">'+
								'<div class="item-left">'+
									'<a href="'+getRealPath()+'/views/blog/theblog.html?'+list[i].userdetail.id+'">'+
									'<img src="'+getRealPath()+list[i].userdetail.head+'"/></a>'+
									'<span>'+list[i].userdetail.name+'</span>'+
								'</div>'+
								'<div class="item-right">'+
									'<div class="article-header">'+
										'<h1><a href="'+getRealPath()+'/views/article/thearticle.html?'+list[i].article.id+'">'+
										list[i].article.title+'</a></h1>'+
										'<span>发布时间：'+list[i].article.createtime.substr(0,16)+'</span>'+
									'</div>'+
									'<div class="article-info">'+
										'<p>'+list[i].article.info+'</p>'+
									'</div>'+
									'<div class="article-others">'+
										'<div class="tags">'+
											tags+
										'</div>'+
										'<div class="time-count">'+
											'<span>打赏：</span><i>'+list[i].article.reward+'</i>'+
											'<span>收藏：</span><i>'+list[i].article.collecting+'</i>'+
											'<span>阅读量：</span><i>'+list[i].article.reading+'</i>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>');
	}
}
function showQuestion(list){
	for(var i = 0; i<list.length; i++){
		var tags = "";
		for(var j = 0; j<list[i].tag.length; j++){
			tags = tags + '<a href="'+getRealPath()+'/views/tag/thetag.html?'+list[i].tag[j].id+'"><span>'+list[i].tag[j].name+'</span></a>';
		}
		var isAccept = "";
		if(list[i].accept){
			isAccept = "已采纳";
		}else{
			isAccept = "未采纳";
		}
		$(".list .btn-box").before('<div class="aq-item item">'+
								'<div class="item-left">'+
									'<a href="'+getRealPath()+'/views/blog/theblog.html?'+list[i].userdetail_id+'">'+
									'<img src="'+getRealPath()+list[i].path+'"/></a>'+
									'<span>'+list[i].username+'</span>'+
								'</div>'+
								'<div class="item-right">'+
									'<div class="aq-title">'+
										'<h1><a href="'+getRealPath()+'/views/aq/theaq.html?'+list[i].id+'">'+
										list[i].title+'</a></h1>'+
										'<span>发布时间：'+list[i].createtime.substr(0,16)+'</span>'+
									'</div>'+
									'<div class="aq-others">'+
										'<div class="tags">'+
											tags+
										'</div>'+
										'<div class="time-count">'+
											'<span>阅读量：</span><i>'+list[i].reading+'</i>'+
											'<span>回答：</span><i>'+list[i].answer+'</i>'+
											'<span>'+isAccept+'</span>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>');
	}
}
function initTagArticle(){
	$(".list .item").remove();
	currentIndex=1;
	getTagArticle(currentIndex);
}
function initTagQuestion(){
	$(".list .item").remove();
	currentIndex=1;
	getTagQuestion(currentIndex);
}
function getMore(){
	if($(".active>a").text()=="文章"){
		getTagArticle(currentIndex);
	}else{
		getTagQuestion(currentIndex);
	}
}
function findHotTag(){
	var url = "/HyperIT/tag/findTagByArticleNum";
	var parm = "size=20&indexPageNum=1";
	$.get(url,parm,function(data){
		if (data.serviceResult) {
			var taglist = data.resultParm.taglist;
			showTagList(taglist);
		} else{
			messageTip(data.resultInfo);
		}
	});
}
function showTagList(list){
	for(var i = 0; i<list.length; i++){
		$(".wrap-right ul").append('<li>'+
							           '<a href="thetag.html?'+list[i].id+'">'+list[i].name+'</a>'+
						           	   '<span>'+list[i].article+'+'+list[i].question+'</span>'+
						          '</li>');
	}
}
